home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / ttydriv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  2.2 KB  |  112 lines

  1. /* TTY input driver */
  2. #include <stdio.h>
  3. #include "global.h"
  4. #include "mbuf.h"
  5. #include "session.h"
  6. #include "tty.h"
  7. #include "socket.h"
  8.  
  9. #define    OFF    0
  10. #define    ON    1
  11.  
  12.  
  13. #define    LINESIZE    256
  14.  
  15. #define    CTLU    21
  16. #define CTLR    18
  17. #define    CTLZ    26
  18.  
  19. /* Accept characters from the incoming tty buffer and process them
  20.  * (if in cooked mode) or just pass them directly (if in raw mode).
  21.  *
  22.  * Note that echoing is direct to the terminal, not through the session
  23.  * output buffering system.
  24.  */
  25.  /*Control-R added by df for retype of lines - useful in Telnet */
  26.  
  27. struct mbuf *
  28. ttydriv(sp,c)
  29. struct session *sp;
  30. char c;
  31. {
  32.     struct mbuf *bp;
  33.     char *cp,*rp;
  34.  
  35.     switch(sp->ttystate.edit){
  36.     case OFF:
  37.         if((bp = alloc_mbuf(1)) == NULLBUF)
  38.             return NULLBUF;
  39.         *bp->data = c;
  40.         bp->cnt = 1;
  41.         if(sp->ttystate.echo)
  42.             usputc(sp->output,c);
  43.         return bp;
  44.     case ON:
  45.         if(sp->ttystate.line == NULLBUF
  46.          && (sp->ttystate.line = alloc_mbuf(LINESIZE)) == NULLBUF)
  47.             return NULLBUF;
  48.  
  49.         bp = sp->ttystate.line;
  50.         cp = bp->data + bp->cnt;
  51.         /* Perform cooked-mode line editing */
  52.         switch(c & 0x7f){
  53.         case '\r':    /* CR and LF both terminate the line */
  54.         case '\n':
  55.             if(sp->ttystate.crnl)
  56.                 *cp = '\n';
  57.             else
  58.                 *cp = c;
  59.             if(sp->ttystate.echo)
  60.                 usputc(sp->output,'\n');
  61.             bp->cnt += 1;
  62.             sp->ttystate.line = NULLBUF;
  63.             return bp;
  64.         case '\b':        /* Backsp->ttystateace */
  65.             if(bp->cnt != 0){
  66.                 bp->cnt--;
  67.                 if(sp->ttystate.echo)
  68.                     usputs(sp->output,"\b \b");
  69.             }
  70.             break;
  71.         case CTLR:    /* print line buffer */
  72.             if(sp->ttystate.echo){
  73.                 usputs(sp->output,"^R\n") ;
  74.                 rp = bp->data;
  75.                 while (rp < cp)
  76.                     usputc(sp->output,*rp++) ;
  77.             }
  78.             break ;
  79.         case CTLU:    /* Line kill */
  80.             while(bp->cnt != 0){
  81.                 bp->cnt--;
  82.                 if(sp->ttystate.echo){
  83.                     usputs(sp->output,"\b \b");
  84.                 }
  85.             }
  86.             break;
  87.         default:    /* Ordinary character */
  88.             *cp = c;
  89.             bp->cnt++;
  90.  
  91.             /* ^Z apparently hangs the terminal emulators under
  92.              * DoubleDos and Desqview. I REALLY HATE having to patch
  93.              * around other people's bugs like this!!!
  94.              */
  95.             if(sp->ttystate.echo &&
  96. #ifndef    AMIGA
  97.              c != CTLZ &&
  98. #endif
  99.              bp->cnt < LINESIZE-1){
  100.                 usputc(sp->output,c);
  101.  
  102.             } else if(bp->cnt >= LINESIZE-1){
  103.                 usputc(sp->output,'\007');    /* Beep */
  104.                 bp->cnt--;
  105.             }
  106.             break;
  107.         }
  108.         break;
  109.     }
  110.     return NULLBUF;
  111. }
  112.